package com.nb.cloud.doc.controller;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nb.cloud.doc.entity.Document;
import com.nb.cloud.doc.entity.TextbookChapter;
import com.nb.cloud.doc.service.IDocumentService;
import com.nb.cloud.doc.service.ITextbookChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 作者
 * @since 2024-07-21
 */
@RestController
@RequestMapping("/document")
public class DocumentController {

    @Autowired
    ITextbookChapterService chapterService;

    @Autowired
    IDocumentService documentService;

    @GetMapping("get")
    public String get(){
        String cookie = "__51vuft__Jk6DBsTOLgUIe7oC=1657263877688; __51vcke__Jk6DBsTOLgUIe7oC=e7e706cb-dbb2-5192-b372-7c803ae72c0e; XDFUUID=f6685bc6-475c-58b2-90fe-cdb5c23603cd; bb774449518827a0_gr_last_sent_cs1=lihaoyang6@xdf.cn; bb774449518827a0_gr_cs1=lihaoyang6@xdf.cn; 91c8d7964b724d0a_gr_last_sent_cs1=73966f26ef323a13b7c75c506be19544; 964de61476ecd75d_gr_last_sent_cs1=lihaoyang6; Hm_lvt_e010d1faf316a4dbfe8639481a2a3f90=1713767355,1714964844; 964de61476ecd75d_gr_cs1=lihaoyang6; b30013a7e5dcf01f_gr_last_sent_cs1=lihaoyang6@xdf.cn; x2g23=7I-jf8i6B5A; x2acA=HtMIHbb8KAmRtOo%2BKtFn%2BA%3D%3D; 91c8d7964b724d0a_gr_cs1=73966f26ef323a13b7c75c506be19544; gr_user_id=b9143eba-9c87-4b8f-9bde-5f2dc18a6530; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22190ba85a5d349-0ac88efa5e0dca-1f343371-921600-190ba85a5d46a7%22%2C%22first_id%22%3A%22%22%2C%22props%22%3A%7B%7D%2C%22%24device_id%22%3A%2218ef961bb0a3cc-0689f60b4cf381-1f343371-921600-18ef961bb0bb8b%22%7D; erpUserId=lihaoyang6; jiaowuSchoolId=undefined; erpSchoolId=1; xeasy_st=0b309916c17d446b8ac10668916c4a40; xeasy_e2e=717241408BCF14627222D00FB00BB39B; xeasy_x3mf=d563177904db47c5bd54ec92ddda612f; xeasy_user_email=lihaoyang6@xdf.cn; xeasy_user_name=%E6%9D%8E%E6%B5%A9%E6%B4%8B; b30013a7e5dcf01f_gr_cs1=lihaoyang6@xdf.cn; returnUrl=/account; E2Email=lihaoyang6@xdf.cn; U2NickName=%E6%9D%8E%E6%B5%A9%E6%B4%8B; 863788c8cb539c69_gr_last_sent_cs1=lihaoyang6@xdf.cn; 863788c8cb539c69_gr_cs1=lihaoyang6@xdf.cn; __51uvsct__Jk6DBsTOLgUIe7oC=101; __vtins__Jk6DBsTOLgUIe7oC=%7B%22sid%22%3A%20%22b53adb85-85de-533e-9fd4-4befd6c5c96b%22%2C%20%22vd%22%3A%204%2C%20%22stt%22%3A%201161183%2C%20%22dr%22%3A%202620%2C%20%22expires%22%3A%201721538697908%2C%20%22ct%22%3A%201721536897908%7D; session_token=eyJ1c2VySWQiOiI2MTdmNTZiZmU0YjBmNmEzOGMyZWY5NzUiLCJuYW1lIjoiU2Fv5qyjIiwiYXBpdG9rZW4iOiJhcGlfYjYxMjhhNTdlYzEzNDI4ZmIzZDkzOTZjMTkwOTk0ZDciLCJwaG9uZU51bSI6IjE4MzEwODQzNjEwIiwiaXNWaXAiOnRydWUsInZpcEV4cGlyZVRpbWUiOjE4Njc4NTI3OTkwMDAsInZpcEV4cGlyZURhdGUiOm51bGwsImNvb2tpZUV4cGlyZVRpbWUiOjE3MjI4MzI4OTU4MzQsImRvd25sb2FkQ291bnRMZWZ0IjpudWxsLCJkb3dubG9hZENvdW50IjpudWxsLCJuZXdSZWciOmZhbHNlLCJsb2dpblN0YXR1cyI6MCwidmlwU3RhdHVzIjowLCJpc1Nob3dHdWlkZSI6ZmFsc2UsImRvd25sb2FkVHlwZSI6MCwiaGFzUGFzc3dvcmQiOnRydWUsImhvbWVQYWdlR3JheSI6MCwidmVyaWZ5VmVyc2lvblN0YXR1cyI6MCwiZ3JhZGVDb2RlIjoiZ3JhZGVfMSIsImdyYWRlTmFtZSI6bnVsbCwic2VtZXN0ZXJDb2RlIjoic2VtZXN0ZXJfMSIsInByaW50ZXJCaW5kU3RhdHVzIjowLCJ0cmFjZVVpZCI6bnVsbH0%3D";
        //{"atlasLibId":"72c5649c59ef4d2d88529155fd732cd1","pageSize":20,"pageNum":1,"hardLevelSort":0}

        String url = "https://ys.xdf.cn/api/atlas/lib/getSixSevenFileList";

        //查询章节
        LambdaQueryWrapper<TextbookChapter> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(TextbookChapter::getParentId,"-1")
                .orderByAsc(TextbookChapter::getId);
        List<TextbookChapter> chapterList = chapterService.list(wrapper);
        for(TextbookChapter chapter : chapterList){
            JSONObject obj = new JSONObject();
            obj.set("atlasLibId",chapter.getChapterId());
            obj.set("pageNum",1);
            obj.set("pageSize",10000);

            String resp = HttpRequest.post(url)
                    .body(obj.toString())
                    .execute()
                    .body();
            System.out.println(resp);

            JSONObject respObj = JSONUtil.parseObj(resp);
            JSONObject data = respObj.getJSONObject("data");
            JSONArray dataList = data.getJSONArray("dataList");

            String chapterId = chapter.getChapterId();
            List<Document> documentList = new ArrayList<>();
            for(Object objData : dataList){
                JSONObject docObj = (JSONObject)objData;
                //2-文档  3-合集 4-攻略
                Integer type = docObj.getInt("type");
                //文档类型(1-文档；2-合集；3-攻略)
                if(type==2){
                    type=1;
                }else if(type==3){
                    type=2;
                } else if(type==4){
                    type=3;
                }
                String coverImg = docObj.getStr("coverImg");
                String title = docObj.getStr("title");
                Integer hardLevel = docObj.getInt("hardLevel");
                String description = docObj.getStr("description");
                String fullIdPath = docObj.getStr("fullIdPath");
                String docId = docObj.getStr("atlasLibId");

                Document doc = Document.builder()
                        .documentId(docId)
                        .chapterId(chapterId)
                        .docType(type)
                        .title(title)
                        .description(description)
                        .coverImage(coverImg)
                .difficulty(hardLevel)
                .fullIdPath(fullIdPath)

                .grade(chapter.getGrade())
                .subjectCode(chapter.getSubjectCode())
                .term(chapter.getTerm())
                .textbookVersionId(chapter.getTextbookVersionId())
                .status(1)
                .deleteFlag(0).build();
                documentList.add(doc);
                //构造文件详情
            }

            documentService.saveBatch(documentList);


        }
        return "OK";
    }

}
